package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.ReportListAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.ReporterListMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Yxd on 2018/3/22.
 */

public class HealthInquiriesActivity extends BaseActivity implements CallBack {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.top_text)
    TextView topText;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    private RecyclerView.ViewHolder viewHolder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_inquiries);
        ButterKnife.bind(this);
        initView();
        initData();
        setOnClick();
    }

    private void setOnClick() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    private void initView() {
        topText.setText("健康报告");
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                recyclerView.setEnabled(topRowVerticalPosition >= 0);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
    }

    private void initData() {
        OkGo.post("http://admin.3dweijing.com/admin.php/api/member/healthReport")
                .params("user_tel", Singleton.getInstance().getUser().getUser_telnum())
                .tag(HealthInquiriesActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        swipeRefreshLayout.setRefreshing(false);
                        Log.e("--", "onSuccess: " + s);
                        ReporterListMsg msg = new Gson().fromJson(s, ReporterListMsg.class);
                        if (msg.getFlag()) {
                            if (msg.getList() != null && msg.getList().size() > 0) {
                                resautStatus.setVisibility(View.GONE);
                                recyclerView.setAdapter(new ReportListAdapter(msg.getList(),HealthInquiriesActivity.this));
                            } else {
                                resautStatus.setVisibility(View.VISIBLE);
                            }
                        }else {
                            resautStatus.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void doNext(int what, Object obj) {
        Log.e("--", "doNext: "+what);
        ReporterListMsg.ListBean b= (ReporterListMsg.ListBean) obj;
        Intent intent =new Intent(HealthInquiriesActivity.this,ReporterinfoActivity.class);
        intent.putExtra("user_tel",b.getUser_tel());
        intent.putExtra("create_time",b.getCreate_time());
        startActivity(intent);
    }
}
